﻿using System;
using System.Linq;
using System.Text;
using System.ServiceModel.PeerResolvers;
using log4net;
using System.ServiceModel;

namespace WCFPeerResolver
{
    /// <summary>
    /// Peer resolver class handling communication between the proxy and the storage.
    /// </summary>
    public class WcfPeerResolver : IPeerResolverContract
    {
        private readonly IPeerStorageManager m_Storage;
        private readonly ILog m_Log = LogManager.GetLogger("Resolver");

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="storage">Storage manager to be used to store mesh information.</param>
        public WcfPeerResolver(IPeerStorageManager storage)
        {
            m_Storage = storage;
        }


        RegisterResponseInfo IPeerResolverContract.Register(RegisterInfo registerInfo)
        {
            try
            {
                return m_Storage.Register(registerInfo);
            }
            catch (Exception e)
            {
                m_Log.Error(e.Message, e);
                throw new FaultException(e.Message);
            }
        }

        RegisterResponseInfo IPeerResolverContract.Update(UpdateInfo updateInfo)
        {
            try
            {
                return m_Storage.Update(updateInfo);
            }
            catch (Exception e)
            {
                m_Log.Error(e.Message, e);
                throw new FaultException(e.Message);
            }
        }

        ResolveResponseInfo IPeerResolverContract.Resolve(ResolveInfo resolveInfo)
        {
            try
            {
                return m_Storage.Resolve(resolveInfo);
            }
            catch (Exception e)
            {
                m_Log.Error(e.Message, e);
                throw new FaultException(e.Message);
            }
        }

        void IPeerResolverContract.Unregister(UnregisterInfo unregisterInfo)
        {
            try
            {
                m_Storage.Unregister(unregisterInfo);
            }
            catch (Exception e)
            {
                m_Log.Error(e.Message, e);
                throw new FaultException(e.Message);
            }
        }

        RefreshResponseInfo IPeerResolverContract.Refresh(RefreshInfo refreshInfo)
        {
            try
            {
                return m_Storage.Refresh(refreshInfo);
            }
            catch (Exception e)
            {
                m_Log.Error(e.Message, e);
                throw new FaultException(e.Message);
            }
        }

        ServiceSettingsResponseInfo IPeerResolverContract.GetServiceSettings()
        {
            try
            {
                return m_Storage.GetServiceSettings();
            }
            catch (Exception e)
            {
                m_Log.Error(e.Message, e);
                throw new FaultException(e.Message);
            }
        }
    }
}
